﻿using Base.ClientUI;
using Base.Common.dto;
using Base.WCS.Crane;
using Base.WCS.Entity;
using GK.WCS.Client.Station;

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Media.Animation;
using System.Windows.Media.Media3D;

namespace GK.WCS.Client
{
    public partial class FrmCranes : UITitlePage
    {
        private int craneId = 91;
        string idle = "点击恢复执行";
        string working = "点击循环停止";
        public FrmCranes()
        {
            InitializeComponent();
            List<String> list = new List<string>();
            list.Add("行走");
            list.Add("取");
            list.Add("放");

            List<String> listPai = new List<string>();
            listPai.Add("1");
            listPai.Add("2");
            listPai.Add("3");
            listPai.Add("4");
            TxtStartPai.DataSource = listPai;

        }


        private void LabClearAlarm_Click(object sender, EventArgs e)
        {
            bool result = HttpCraneUtil.ClearCraneFault(craneId);
            if (result)
            {
                this.ShowSuccessDialog("解警成功");
            }
            else
            {
                this.ShowErrorDialog("解警失败");
            }
        }

        private void LabChange_Click(object sender, EventArgs e)
        {

            if (working == LabChange.Text)
            {
                HttpParam.UpdateCraneOverStop(craneId, 1);
            }
            else
            {
                HttpParam.UpdateCraneOverStop(craneId, 0);
            }

            FrmCranes_Load(null, null);
        }

        private void LabStop_Click(object sender, EventArgs e)
        {
           bool b =  HttpCraneUtil.EmergencyStop(craneId);
            if (b)
            {
                this.ShowSuccessDialog("急停成功");
            }
            else
            {

                this.ShowSuccessDialog("急停失败");
            }
        }

        private void LabDeleteTask_Click(object sender, EventArgs e)
        {
            HttpCraneUtil.ClearCraneTask(craneId);
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            try
            {
                CraneStatusGk craneData = HttpCraneUtil.loadData(craneId);
                uiGroupBox1.Text = (craneId - 90) + "号垛机状态信息";
                if (craneData == null)
                {
                    this.uiGroupBox1.Controls.Clear();
                    GenerateMsg(0, "链接服务器异常");
                    return;
                }
                else
                {
                    this.uiGroupBox1.Controls.Clear();
                }
                ShowInfo(craneData, craneId);
                BindLabel(craneData);
            }
            catch (Exception ex)
            {
                this.ShowErrorNotifier(ex.Message);
            }
        }



        private void BindLabel(CraneStatusGk craneData)
        {
            string name = "模式";
            int height = 0;
            if (craneData.isfault())
            {
                GenerateMsg(height++, "请解警");
            }
            if (craneData.craneMode == 1)
            {
                GenerateMsg(height++, "自动"+ name);
            }
            else if (craneData.craneMode == 2)
            {
                GenerateMsg(height++, "半自动" + name);
            }
            else if (craneData.craneMode == 3)
            {
                GenerateMsg(height++, "手动" + name);
            }
            else if (craneData.craneMode == 4)
            {
                GenerateMsg(height++, "维修" + name);
            }
            else
            {
                GenerateMsg(height++, "未知" + craneData.craneMode);
            }

            List<String> info = craneData.getInf();

            foreach (var item in info)
            {
                GenerateMsg(height++, item);
            }
        }
        private void GenerateMsg(int index, string text)
        {
            var LabMsg1 = new UILabel();
            LabMsg1.AutoSize = true;
            LabMsg1.Font = new System.Drawing.Font("微软雅黑", 15F);
            LabMsg1.Location = new System.Drawing.Point(10, 35 + index * 40);
            LabMsg1.Name = "LabMsg" + index;
            LabMsg1.Size = new System.Drawing.Size(0, 27);
            LabMsg1.Style = UIStyle.Custom;
            LabMsg1.TabIndex = index;
            LabMsg1.Text = text;
            LabMsg1.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.uiGroupBox1.Controls.Add(LabMsg1);
        }
        private void ShowInfo(CraneStatusGk craneData, int craneId)
        {
            uiLabelTaskNo.Text = craneData.finishTaskNo.ToString();
            uiLabelStatus.Text = TaskUtil.getDisplayName("crane.status.", craneData.finishFlag + "");
        }
        
        private void LabExecute_Click(object sender, EventArgs e)
        {
            if (startLoc.Text.Length < 2) {
                this.ShowSuccessDialog("取货位置异常");
                return;
            }
            if (endLoc.Text.Length < 2) {
                this.ShowSuccessDialog("放货位置异常");
                return;
            }
            Task.Run(() => { 
                String info = HttpCraneUtil.sendTaskLoc( startLoc.Text, endLoc.Text);
                this.ShowSuccessDialog(info);
                
            });
            
        }

       
        

        private void FrmCranes_Load(object sender, EventArgs e)
        {
            int craneStop = HttpParam.GetCraneOverStop(craneId);
            this.Invoke(new Action(() =>
            {
                if (craneStop == 1)
                {
                    LabChange.Text = idle;
                }
                else if (craneStop == 0)
                {
                    LabChange.Text = working;
                }
            }));
            init();
        }



        private void uiButton1_Click(object sender, EventArgs e)
        {
            uiButton1.FillColor = System.Drawing.Color.Blue;
            uiButton2.FillColor = System.Drawing.Color.Silver;
            craneId = 91;
            timer1_Tick(null, null);
            FrmCranes_Load(null, null);
        }

        private void uiButton2_Click(object sender, EventArgs e)
        {
            uiButton2.FillColor = System.Drawing.Color.Blue;
            uiButton1.FillColor = System.Drawing.Color.Silver;
            craneId = 92;
            timer1_Tick(null, null);
            FrmCranes_Load(null,null);
        }

        private void in3_CheckedChanged(object sender, EventArgs e)
        {
            var r = (RadioButton)sender;

            TxtStartPai.Enabled = r.Checked;
            TxtStartLie.Enabled = r.Checked;
            TxtStartCeng.Enabled = r.Checked; 
            setData();
        }

        private void out3_CheckedChanged(object sender, EventArgs e)
        {
            var r = (RadioButton)sender;

            TxtEndPai.Enabled = r.Checked;
            TxtEndLie.Enabled = r.Checked;
            TxtEndCeng.Enabled = r.Checked;
            setData();
        }

       
        private void TxtStartPai_SelectedIndexChanged(object sender, EventArgs e)
        {

            
            setData();
        }
        private void TxtEndPai_SelectedIndexChanged(object sender, EventArgs e)
        {
            
            setData();
        }
        




        private void uiLabelTaskNo_Click(object sender, EventArgs e)
        {
            Label b = (Label)sender;

       
        }

        private void in1_CheckedChanged(object sender, EventArgs e)
        {
          
            setData();

        }

        private void out1_CheckedChanged(object sender, EventArgs e)
        {
            setData();
        }

        private void uiButton3_Click(object sender, EventArgs e)
        {
            bool b = HttpCraneUtil.Reset(craneId);
            if (b)
            {
                this.ShowSuccessDialog("复位成功");
            }
            else
            {

                this.ShowSuccessDialog("复位失败");
            }
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e) {
            setData();
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e) {
            setData();
        }
        

        int getshelfId(int crane,int xd, int pi,int lie,int cen) {
            int selfId = 0;
            if (pi < 3) {
                selfId = 1;
            } else {
                selfId = 2;
            }
            selfId=(crane - 1) * 4 + (xd - 1) * 2 + selfId;
            int deep = 0;
            if (pi == 1 || pi == 4) {
                deep = 2;
            } else {
                deep = 1;
            } 
            return selfId * 100000 + lie * 1000 + cen * 10 + deep;

        }

        void getSrc(int crane,int xd) {

            int src = 0;
            if (pick1.Checked) {
                src = (2 - crane) * 20 + (3 - xd) * 10 + 1;
            } else if (pick2.Checked) {
                src = (2 - crane) * 20 + (3 - xd) * 10 + 4;
            } else {
                int pi = 0;
                int lie = 0;
                int cen = 0;
                int.TryParse(TxtStartPai.Text, out pi);
                int.TryParse(TxtStartLie.Text, out lie);
                int.TryParse(TxtStartCeng.Text, out cen);
                if (pi == 0 || lie == 0 || cen == 0) {
                    startLoc.Text =  "";
                    return;
                }
                src = getshelfId(crane, xd, pi, lie, cen);
            }
           
            startLoc.Text = src+"";
        }

        void getDes(int crane, int xd) {

            int src = 0;
            if (put1.Checked) {
                src = (2 - crane) * 20 + (3 - xd) * 10 + 1;
            } else if (put2.Checked) {
                src = (2 - crane) * 20 + (3 - xd) * 10 + 4;
            } else {
                int pi = 0;
                int lie = 0;
                int cen = 0;
                int.TryParse(TxtEndPai.Text, out pi);
                int.TryParse(TxtEndLie.Text, out lie);
                int.TryParse(TxtEndCeng.Text, out cen);
                if (pi == 0 || lie == 0 || cen == 0) {
                    endLoc.Text = "";
                    return;
                }
                src = getshelfId(crane, xd, pi, lie, cen);
            }
     
            endLoc.Text = src + "";
        }

        void  setData() {
            int crane = dj1.Checked?1:2;  
            int xd = xd1.Checked?1:2; 
            getSrc(crane, xd);
            getDes(crane, xd);
        }

        private void chanal1_CheckedChanged(object sender, EventArgs e) {
            setData();
        }

        private void chanal2_CheckedChanged(object sender, EventArgs e) {
            setData();
        }

        private void radioButton4_CheckedChanged(object sender, EventArgs e) {
            setData();
        }

        private void radioButton3_CheckedChanged(object sender, EventArgs e) {
            setData();
        }

        private void uiButton4_Click(object sender, EventArgs e) {
            if ("正常出库" == uiButton4.Text) {
                HttpParam.updateValue(craneId-90, 1);
               
            } else {
                HttpParam.updateValue(craneId - 90, 0);
            
            }
            init();


        }

        void init() {

            List<SysParam> paramList = HttpParam.getParam();
            if (paramList == null) {
                return;
            }
            foreach (SysParam param in paramList) {
                if (param.id + 90 == craneId) {
                    if (param.v == 1) {
                        uiButton4.Text = "禁止出库";
                    } else {
                        uiButton4.Text = "正常出库";
                    }

                }
            }
        }

        private void uiButton5_Click(object sender, EventArgs e) {

        }
    }
}